/**
 * Copyright (c) 2007,中企动力华南研发中心<br>
 * All rights reserved.<br>
 */
package com.ce.pms.system.renovation.web;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.ce.pms.base.web.PmsBaseAction;
import com.ce.pms.common.model.HmEmployeeDomain;
import com.ce.pms.common.service.IOrganizationalStructureService;
import com.ce.pms.exception.UILayerException;
import com.ce.pms.system.renovation.service.IFitmentApproveSetupService;
import com.ce.pms.system.renovation.vo.FitmentApproveSetupVO;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.annotation.Read;
import com.sitechasia.webx.core.annotation.Scope;

/**
 * 功能描述：<br>
 * @author 易贤建 创建日期：2009-1-8<br>
 */
public class FitmentApproveSetupAction extends PmsBaseAction {
	/**
	 * 装修审核设置
	 */
	private IFitmentApproveSetupService fitmentApproveSetupService;
	/**
	 * 组织架构Service
	 */
	private IOrganizationalStructureService organizationalStructureService = null;

	private final String CACHELIST = "hei_hei_cache_2_session";
	/**
	 * 功能描述:初始化主页面<br>
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2009-1-8  <br>
	 */
	public String initMain(){
		initData();
		return "main";
	}
	/**
	 * 功能描述:  <br>
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2009-1-8  <br>
	 */
	public String initList(){
		List<FitmentApproveSetupVO> list = fitmentApproveSetupService.findFirstApp();
		setCahceList(list);
		int count = list == null ? 0 : list.size();
		setAttribute("totalRows", Integer.valueOf(count));
		setAttribute("employess", list);
		return "list";
	}
	/**
	 * 功能描述:初始化ADD<br>
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2009-1-8  <br>
	 */
	public String initAdd(@Read(key="second_employeeId")Long second_employeeId){
		setAttribute("second_employeeId", second_employeeId);
		return "add";
	}
	/**
	 * 功能描述:查询列表(从缓存中)<br>
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2009-1-8  <br>
	 */
	public String list(){
		List<FitmentApproveSetupVO> list = processList(getCacheList());
		int count = list == null ? 0 : list.size();
		setAttribute("totalRows", Integer.valueOf(count));
		setAttribute("employess", list);
		return "list";
	}

	/**
	 * 功能描述:添加选择的第一级审批人到缓存<br>
	 * 添加到缓存,设置状态为添加
	 * @param setupVO
	 * Create author:yixianjian <br>
	 * Create on:2009-1-8  <br>
	 */
	public String addEmployee2Cache(@Read FitmentApproveSetupVO setupVO,@Read(key="second_employeeId")Long second_employeeId){
		validationEmployeeExistCache(setupVO,second_employeeId);
		setupVO.setCreator(super.getUserId());
		setupVO.setIsAdd(true);
		getCacheList().add(setupVO);
		setAttribute("option", "windowClose"); //设置标shi 关闭
		return "result";
	}

	/**
	 * 功能描述:检查一级审批是否是最后一条数据   <br>
	 *
	 * Create author:黄彦军 <br>
	 * Create on:2009-1-21  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public void checkDelete(){
		List<FitmentApproveSetupVO> list = getCacheList();
		int count=0;
		for (FitmentApproveSetupVO setupVO : list) {
			if(!setupVO.getIsDelete()){
				count ++;
			}
		}

		if(count==1){
			this.renderText("false");
		}else{
			this.renderText("true");
		}
	}

	/**
	 * 功能描述:检查二级审批人（不能在一级审批人中有）  <br>
	 * @param second_employeeId
	 * Create author:黄彦军 <br>
	 * Create on:2009-1-21  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public void checkSecondEmployee(@Read(key="second_employeeId")Long secondEmployeeId){
		List<FitmentApproveSetupVO> list = getCacheList();
		for (FitmentApproveSetupVO setupVO : list) {
			if(!setupVO.getIsDelete() && secondEmployeeId.equals(setupVO.getEmployeeId())){
				this.renderText("装修申请审批人员不能在两级审批设置中同时设置!");
				return ;
			}
		}
		this.renderText("true");
	}

	/**
	 * 功能描述: 检查数据有没有发生变化 <br>
	 * @param secondEmployeeId
	 * @param depositEmployeeId
	 * Create author:黄彦军 <br>
	 * Create on:2009-1-22  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public void checkData(@Read(key="secondEmployeeId")Long secondEmployeeId,
			@Read(key="depositEmployeeId")Long depositEmployeeId){

		FitmentApproveSetupVO second = fitmentApproveSetupService.findSecondApp();
		FitmentApproveSetupVO deposit = fitmentApproveSetupService.findDepositApp();
		if(!secondEmployeeId.equals(second.getEmployeeId())
		   || !depositEmployeeId.equals(deposit.getEmployeeId())){
			this.renderText("false"); // 改变了
			return ;
		}
		boolean flag = checkFirst();
		if(flag)
			this.renderText("true");
		else
			this.renderText("false");
	}

	/**
	 * 功能描述:检查一级审批人是否发生了改变  <br>
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2009-1-22  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private boolean checkFirst(){
		List<FitmentApproveSetupVO> firstList = fitmentApproveSetupService.findFirstApp();
		List<FitmentApproveSetupVO> cacheList =  getCacheList();
		Set<Long> firstOld = new HashSet<Long>();
		for (FitmentApproveSetupVO first : firstList) {
			firstOld.add(first.getEmployeeId());
		}

		Set<Long> firstNew = new HashSet<Long>();
		for (FitmentApproveSetupVO cache : cacheList) {
			if(!cache.getIsDelete())
				firstNew.add(cache.getEmployeeId());
		}

		if(firstOld.size() >= firstNew.size()){
			for (Long old : firstOld) {
				if (!firstNew.contains(old)) {
					return false;
				}
			}
		}else{
			for (Long newFir : firstNew) {
				if (!firstOld.contains(newFir)) {
					return false;
				}
			}
		}

		return true;
	}

	/**
	 * 功能描述:删除缓存中的对应的VO<br>
	 * 设置状态为删除
	 * @param setupVO
	 * Create author:yixianjian <br>
	 * Create on:2009-1-8  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public void delete(@Read FitmentApproveSetupVO setupVO){
		List<FitmentApproveSetupVO> list = getCacheList();
		list.get(list.indexOf(setupVO)).setIsDelete(true);
	}
	/**
	 * 功能描述:更新数据<br>
	 * Create author:yixianjian <br>
	 * Create on:2009-1-8  <br>
	 */
	public String update(){
		FitmentApproveSetupVO second = getFitment("second");
		FitmentApproveSetupVO deposit = getFitment("deposit");

		fitmentApproveSetupService.update(getCacheList(), second, deposit);
		initData();
		return "main";
	}

	/**
	 * 功能描述:通过员工ID获取员工JOB<br>
	 * 可以考虑在选员工时,就有员工职位选择,可以不要这个方法
	 * @param employeeId
	 * Create author:yixianjian <br>
	 * Create on:2009-1-8  <br>
	 */
	public void getJob(@Read(key="employeeId")Long employeeId){
		if(employeeId == null) return;

		HmEmployeeDomain employee = organizationalStructureService.getHmEmployeeDomainById(employeeId);
		if(employee != null)
			renderText(employee.getJob());
	}
	/**
	 * 功能描述:  <br>
	 * @param employeeId
	 * Create author:yixianjian <br>
	 * Create on:2009-1-8  <br>
	 */
	public void validationEmployeeExistCache(@Read FitmentApproveSetupVO setupVO,@Read (key="second_employeeId")Long second_employeeId){
		List<FitmentApproveSetupVO> list = getCacheList();
		for (FitmentApproveSetupVO cache : list) {
			if(setupVO.getEmployeeId().equals(cache.getEmployeeId()) && !cache.getIsDelete())
				throw new UILayerException("已经存在该员工",UILayerException.FAILURE);
		}

//		boolean flag = list.contains(setupVO);
//		if(flag)
//			throw new UILayerException("已经存在该员工",UILayerException.FAILURE);

		if(second_employeeId != null && second_employeeId.equals(setupVO.getEmployeeId()))
			throw new UILayerException("装修申请审批人员不能在两级审批设置中同时设置！",UILayerException.FAILURE);
	}
	/**
	 * 功能描述:过滤掉删除状态的VO<br>
	 * @param list
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2009-1-9  <br>
	 */
	private List<FitmentApproveSetupVO> processList(List<FitmentApproveSetupVO> list){
		List<FitmentApproveSetupVO> temp = new ArrayList<FitmentApproveSetupVO>();
		if(list == null) return null;
		for(FitmentApproveSetupVO vo : list){
			// 如果是删除状态直接跳过
			if( vo.getIsDelete()) continue;
			temp.add(vo);
		}
		return temp;
	}
	/**
	 * 功能描述:获取第二装修审批人<br>
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2009-1-8  <br>
	 */
	private FitmentApproveSetupVO getFitment(String type){
		FitmentApproveSetupVO vo = new FitmentApproveSetupVO();
		String employeeId = getRequest().getParameter(type + "_employeeId");
		vo.setEmployeeId(Long.valueOf(employeeId));

		String employeeName = getRequest().getParameter(type + "_employeeName");
		vo.setEmployeeName(employeeName);

		String sysrefcodeID = getRequest().getParameter(type + "_sysrefKeyword");
		vo.setSysrefKeyword(Long.valueOf(sysrefcodeID));

		vo.setModifier(super.getUserId());

		return vo;
	}
	/**
	 * 功能描述:初始化第二次审批的人<br>
	 * 装修保证金审核的人
	 * Create author:yixianjian <br>
	 * Create on:2009-1-8  <br>
	 */
	private void initData(){
		FitmentApproveSetupVO deposit = fitmentApproveSetupService.findDepositApp();
		setAttribute("deposit", deposit);

		FitmentApproveSetupVO second = fitmentApproveSetupService.findSecondApp();
		setAttribute("second", second);
	}
	/**
	 * 功能描述:从session中获取缓存的LIST<br>
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2009-1-8  <br>
	 */
	@SuppressWarnings("unchecked")
	private List<FitmentApproveSetupVO> getCacheList(){
		List<FitmentApproveSetupVO> list = (List<FitmentApproveSetupVO>)getAttribute(this.CACHELIST,Scope.SESSION);
		return list == null ? new ArrayList<FitmentApproveSetupVO>(): list;
	}
	/**
	 * 功能描述:设置缓存<br>
	 * @param list
	 * Create author:yixianjian <br>
	 * Create on:2009-1-8  <br>
	 */
	private void setCahceList(List<FitmentApproveSetupVO> list){
		setAttribute(this.CACHELIST, list,Scope.SESSION);
	}
	public void setFitmentApproveSetupService(
			IFitmentApproveSetupService fitmentApproveSetupService) {
		this.fitmentApproveSetupService = fitmentApproveSetupService;
	}
	public void setOrganizationalStructureService(
			IOrganizationalStructureService organizationalStructureService) {
		this.organizationalStructureService = organizationalStructureService;
	}
}
